Ext.define('AM.controller.UserController', {
    extend: 'Ext.app.Controller',
	views:['MainView','UserList','UserDetailView','UserDetailForm'],
	stores:["UserStore"],
	models:["User"],
	refs:[{
		ref:'userList',
		selector:'userlist'
	},{
		ref:'userDetailView',
		selector:'userdetailview'
	},{
		ref:'userDetailForm',
		selector:'userdetailform'
	}],
	commonDoActionUtil:Ext.create('AM.util.CommonDoActionUtil'),
	onLaunch:function(){
		var store = this.getUserList().getStore();
		store.on('load',function(){
			Ext.getCmp('userlist-grid').getSelectionModel().select(0);	
		});
	},
	init:function(){
		this.control({
			'userlist':{
				beforerender:function(){
					var sm = this.getUserList().getSelectionModel();
					sm.on('selectionchange',function(sm,rs,ops){
						if(rs.length){
							Ext.getCmp("userdetailform-form").setActiveRecord(rs[0]);
						}
					});
				},
				viewready:function(view){
//					console.log(this.getUserList().getSelectionModel().select(0));
//					this.getUserList().getSelectionModel().select(0);					
				}
			},
			'userlist button[id=new]':{
				click:function(o){
				}
			},
			'userlist button[id=update]':{
				click:function(o){
					Ext.Msg.alert("","update");
				}
			},
			'userlist button[id=delete]':{
				click:function(o){
					var grid = o.ownerCt.ownerCt;
					var sm = grid.getSelectionModel();//得到选择模式,此外为checkBoxModel
					var selectItems = sm.getSelection();
					if(selectItems.length == 0){
						Ext.Msg.alert("提示","您最少要选择一条数据");
					}else{
						//得到数组的主键，本处为name
						var store = grid.getStore();
						var ids = [];
						Ext.Array.each(selectItems,function(record){
							ids.push(record.get('name'));
						});
						//向服务器发起ajax请求
						Ext.Ajax.request({
							url:'/laoluo-erp/lesson!deleteUsers.action',
							params:{ids:ids.join(",")},
							timeout:3000,
							method:"POST",
							success:function(response){
								alert(response.responseText);
								Ext.Array.each(selectItems,function(item){
									store.remove(item);//刷新store
								});
							}
						});
						
					}
				}
			},
			'userlist button[id=cancel]':{
				click:function(o){
					var grid = o.ownerCt.ownerCt;
					var sm = grid.getSelectionModel();//得到选择模式,此外为checkBoxModel
//					sm.deselect(0);//第一行取消选择
					alert(sm.getLastSelected().get('name'));
					
				}
			},
			'userlist button[id=search]':{
				click:function(o){
					var keyWordInput = o.ownerCt.queryById("keyWord");
					var keyWord = keyWordInput.getValue();					
					var store = this.getUserList().getStore()
					var lastKeyWord = store.getProxy().extraParams.keyword;
					store.getProxy().extraParams = {'keyword':keyWord};
					
					if(keyWord != lastKeyWord){//判断探索条件是否变化，如果变化page应为1
						store.loadPage(1);
					}else{
						store.load();
					}										
				}
			},
			'userdetailform button[id=save]':{
				click:function(o){
					var parse = new Object();
					parse.grid=this.getUserList();
					parse.id='id';
					this.commonDoActionUtil.doInsert(this.getUserDetailForm(),parse);
				}
			},
			'userdetailform button[id=reset]':{
				click:function(o){
					//从Form中更新现有的Record
					var frm = this.getUserDetailForm();				
					var active = frm.activeRecord;
		            var form = frm.getForm();		
			        if (!active) {
			            return;
			        }
			        if (form.isValid()) {
			            form.updateRecord(active); //重点
			        }
				}
			}
		});
	}
});